﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SIA.Allarms;

namespace SIA.Panels.Allarms
{

    delegate void AddAllarm (IAllarm allarme);

    public partial class ListaAllarmi : Form
    {
        //AllarmManager allarmManager = null;
        private IAllarmsCollections _activeAllarms = null;

        public ListaAllarmi(IAllarmsCollections activeAllarms)
        {
            InitializeComponent();

            this._activeAllarms = activeAllarms;
            this._activeAllarms.RiseONAllarm += new AllarmEvent(_activeAllarms_RiseONAllarm);
            this._activeAllarms.RiseOFFAllarm += new AllarmEvent(_activeAllarms_RiseOFFAllarm);
            this._activeAllarms.RiseACKAllarm += new AllarmEvent(_activeAllarms_RiseACKAllarm);
            this._activeAllarms.RiseAddAllarm += new AllarmEvent(_activeAllarms_RiseAddAllarm);
            this.updateAllarms(_activeAllarms);
        }

        void _activeAllarms_RiseAddAllarm(IAllarm allarm)
        {
            if (!this.IsHandleCreated || !this.Visible) return;
            this.Invoke(new MethodInvoker(delegate
            {
                this.listViewAllarmi.Items.Clear();
                foreach (IAllarm a in _activeAllarms)
                    addAllarm(a);
            }));
        }

        void _activeAllarms_RiseACKAllarm(IAllarm allarm)
        {
            if (!this.IsHandleCreated || !this.Visible) return;
            this.Invoke(new MethodInvoker(delegate
            {
                this.listViewAllarmi.Items.Clear();
                foreach (IAllarm a in _activeAllarms)
                    addAllarm(a);
            }));
        }

        void _activeAllarms_RiseOFFAllarm(IAllarm allarm)
        {
            if (!this.IsHandleCreated || !this.Visible) return;
            this.Invoke(new MethodInvoker(delegate
            {
                this.listViewAllarmi.Items.Clear();
                foreach (IAllarm a in _activeAllarms)
                    addAllarm(a);
            }));
        }

        void _activeAllarms_RiseONAllarm(IAllarm allarm)
        {
            if (!this.IsHandleCreated || !this.Visible) return;
            this.Invoke(new MethodInvoker(delegate
            {
                this.listViewAllarmi.Items.Clear();
                foreach (IAllarm a in _activeAllarms)
                    addAllarm(a);                
            }));
        }

        public void addAllarm(IAllarm a)
        {
           this.listViewAllarmi.Items.Add(new ListViewItem(new string[] { a.Nome, a.Gruppo, a.isActive.ToString(), a.DataOraON.ToString(), a.DataOraOFF.ToString(), a.DataOraACK.ToString(), a.Commento }));
        }

        public void updateAllarms(IAllarmsCollections listaAllarmi)
        {
            try
            {
                this.listViewAllarmi.Items.Clear();
                foreach (IAllarm a in listaAllarmi)
                {
                    addAllarm(a);
                }
            }
            catch (Exception ex)
            {
                SIA.Messages.MessageManager.sendMessage(this, "updateAllarms", ex);
            }
        }

        private void btChiudi_Click(object sender, EventArgs e)
        {            
            this._activeAllarms.sendACK();
            this.Hide();
        }
    }
}
